package com.gosophia.dataDictionary.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseAjaxAction;
import com.gosophia.dataDictionary.dto.DataEntryDTO;
import com.gosophia.dataDictionary.dto.DataValueDTO;
import com.gosophia.dataDictionary.ws.DataDictionaryWebService;

/**
 * 进入编辑词条界面
 * 
 * @author tianchunyu
 * 
 */
@Controller
@Scope("prototype")
public class FindSameValueExceptSelfAction extends BaseAjaxAction<DataEntryDTO> {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public DataEntryDTO getDetailData() {
        if (this.detailData == null) {
            this.detailData = new DataEntryDTO();
        }
        return this.detailData;
    }

    @Autowired
    private DataDictionaryWebService dataDictionaryWebService;

    private Long entryId;

    private String dataValueId;
    private String value;

    private boolean validate;

    @Override
    public String _execute() throws Exception {
        validate = false;
        DataEntryDTO dataEntryDto = dataDictionaryWebService
                .findDataEntryByDataEntryId(entryId);
        List<DataValueDTO> dataValueDtos = dataEntryDto.getDataValues();
        if (dataValueDtos != null && dataValueDtos.size() != 0) {
            for (DataValueDTO dataValueDto : dataValueDtos) {
                if (value.trim().equals(dataValueDto.getValue())
                        && !Long.valueOf(dataValueId).equals(
                                dataValueDto.getDataValueId())) {
                    validate = true;
                }
            }
        }

        super.getRequestResult().setRsltCode("0");
        return SUCCESS;
    }

    public Long getEntryId() {
        return entryId;
    }

    public void setEntryId(Long entryId) {
        this.entryId = entryId;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public boolean isValidate() {
        return validate;
    }

    public void setValidate(boolean validate) {
        this.validate = validate;
    }

    public String getDataValueId() {
        return dataValueId;
    }

    public void setDataValueId(String dataValueId) {
        this.dataValueId = dataValueId;
    }

}
